﻿using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;

public partial class WebUserControl_ControlCapNhatCapBacChoQuanLyTuyenNgoai : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string error = "";
    public DataTable db;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                GetNextMonth();
                GetCB();
            }
            catch (Exception ex)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(ex.Message, method);
            }
        }
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        try
        {
            AddToDataTable();
        }
       
        catch(Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
           
        }
       
    }
    void AddToDataTable()
    {
        
            bool Conflict = false;
            if (Session["DB"] == null)
            {
                CreateDataTable();
                DataRow row = null;
                row = db.NewRow();
                row["MaBUM"] = txtMaBUM.Text.Trim();
                row["HoTen"] = txtHoTenBUM.Text;
                row["CapBac"] = DropCapBac.SelectedItem.ToString();
                row["Rate"] = DropCapBac.SelectedValue.ToString();
                row["Ngay"] = DropThang.SelectedItem.ToString();
                row["Recdate"] = DropThang.SelectedValue.ToString();
                db.Rows.Add(row);

                Session["DB"] = db;

                GridView1.DataSource = db;
                GridView1.DataBind();
            }
            else
            {
                db = (DataTable)Session["DB"];

                foreach (DataRow dr in db.Rows)
                {
                    if ((dr["MaBUM"].ToString() == txtMaBUM.Text.Trim()) && (dr["Ngay"].ToString()==DropThang.SelectedItem.ToString()))
                    {
                        Conflict = true;
                        break;
                    }
                }
                if (Conflict == false)
                {
                    DataRow row = null;
                    row = db.NewRow();
                    row["MaBUM"] = txtMaBUM.Text.Trim();
                    row["HoTen"] = txtHoTenBUM.Text;
                    row["CapBac"] = DropCapBac.SelectedItem.ToString();
                    row["Rate"] = DropCapBac.SelectedValue.ToString();
                    row["Ngay"] = DropThang.SelectedItem.ToString();
                    row["Recdate"] = DropThang.SelectedValue.ToString();
                    db.Rows.Add(row);

                    Session["DB"] = db;

                    GridView1.DataSource = db;
                    GridView1.DataBind();
                }
            }
            Reset();
       
    }
    void Reset()
    {
        txtMaBUM.Text = "";
        txtHoTenBUM.Text = "";
        DropCapBac.SelectedIndex = 0;
    } 
    void CreateDataTable()
    {
        db = new DataTable();
        DataColumn auto = new DataColumn("STT", typeof(System.Int32));
        // specify it as auto increment field
        auto.AutoIncrement = true;
        auto.AutoIncrementSeed = 1;
        auto.ReadOnly = true;
        db.Columns.Add(auto);

        // create another column
        DataColumn salesp_key = new DataColumn("MaBUM", typeof(string));
        db.Columns.Add(salesp_key);

        DataColumn salesp_name = new DataColumn("HoTen", typeof(string));
        db.Columns.Add(salesp_name);

        DataColumn CapBac = new DataColumn("CapBac", typeof(string));
        db.Columns.Add(CapBac);

        DataColumn Rate = new DataColumn("Rate", typeof(string));
        db.Columns.Add(Rate);

        DataColumn Ngay = new DataColumn("Ngay", typeof(string));
        db.Columns.Add(Ngay);

        DataColumn Recdate = new DataColumn("Recdate", typeof(string));
        db.Columns.Add(Recdate);

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string keys = GridView1.DataKeys[e.RowIndex].Value.ToString();
            db = (DataTable)Session["DB"];
            foreach (DataRow dr in db.Rows)
            {
                if (dr["STT"].ToString() == keys)
                {
                    db.Rows.Remove(dr);
                    break;
                }
            }
            Session["DB"] = db;
            GridView1.DataSource = db;
            GridView1.DataBind();
        }
        catch(Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void txtMaBUM_TextChanged(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            string HoTen = dt.GetDataDepend(str, "[ls_getBAName]", CommandType.StoredProcedure, ref error,
                new SqlParameter("@saleSpKey", txtMaBUM.Text.Trim()));
            if (HoTen == "Not Existed")
            {
                uscMsgBox1.AddMessage("Mã BUM không tồn tại trong hệ thống", uscMsgBox.enmMessageType.Error);
            }
            else
            {
                txtHoTenBUM.Text = HoTen;
            }
        }
        catch(Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    void GetNextMonth()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable dbMonth = dt.Get_Data_DataTable(str, ref error, "[ls_getNext3MmYyyy]", CommandType.StoredProcedure);
        DropThang.DataSource = dbMonth;
        DropThang.DataTextField = "oText";
        DropThang.DataValueField = "oVal";
        DropThang.DataBind();
    }
    void GetCB()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable dbCB = dt.Get_Data_DataTable(str, ref error, "[ls_getSalesLevel]", CommandType.StoredProcedure,
            new SqlParameter("@frLevel", 0));
        DropCapBac.DataSource = dbCB;
        DropCapBac.DataTextField = "Description";
        DropCapBac.DataValueField = "Rate";
        DropCapBac.DataBind();
    }
    protected void btUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
               
                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_regSpeBUM]", ref error,
                    new SqlParameter("@salesPKey",GridView1.Rows[i].Cells[1].Text),
                    new SqlParameter("@YyyyMm",GridView1.Rows[i].Cells[6].Text),
                    new SqlParameter("@salesRate",GridView1.Rows[i].Cells[4].Text));
            }
            GridView1.DataSource = null;
            GridView1.DataBind();
            Session["DB"] = null;
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
   
}
